import Vue from 'vue'
import Vuex from 'vuex'
import { getItem, setItem, removeItem, KEY_MAP } from '@/utils/storage'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // 当前登录用户的数据
    user: getItem(KEY_MAP.MY_USER),
    // 缓存的组件名称
    cacheComName: ['Layout']
  },
  mutations: {
    setUserInfo(state, payload) {
      state.user = payload.user

      // 本次暂时存储用户数据
      setItem(KEY_MAP.MY_USER, state.user)
    },
    deleteUserInfo(state) {
      // user 设置为空 清空本地存储
      state.user = null
      removeItem(KEY_MAP.MY_USER)
    },
    // 添加缓存组件名称
    addCacheComName(state, comName) {
      if (!state.cacheComName.includes(comName)) {
        state.cacheComName.push(comName)
      }
    },
    // 移除缓存组件名称
    removeCacheComName(state, comName) {
      const index = state.cacheComName.indexOf(comName)
      if (index !== -1) {
        state.cacheComName.splice(index, 1)
      }
    }
  },
  actions: {},
  modules: {}
})
